/**
 * Package sync list.
 */
require([
  'jquery',
  'pageInit',
  'dataTableFilter',
  'notification',
  'listAsyncOp',
  'loading'
], function($, vamakerPage, VamDataTable, VamOpMsg, VamListOp, VamLoader) {
  $(function() {
    var $dataTable = $('#data-table-list'),
      dataListUrl = VAM.getVar('DATA_LIST_URL'),
      dataSyncOp = VAM.getVar('SYNC_HANDLE_URL'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {},
    listSwitchOp;
    // Table settings.
    tableOptions = {
      // 'serverSide': true,
      'ajax': {
        'url': dataListUrl
      },
      'order':[
        [2, 'desc']
      ],
      'columns':[
        {'data':'name'},
        {'data': 'recentUpdate'},
        {'data': 'modifiedDate'},
        {'data':'operate'}
      ],
      'columnDefs':[
        // {
        //   'targets': 0,
        //   'render': function (data, type, full, meta) {
        //     var resultStr, classStr = '', textStr = '';
        //     if (full.type === 'banner') {
        //       classStr = 'label label-info';
        //       textStr = 'Banner Package';
        //     } else if (full.type === 'video') {
        //       classStr = 'label label-primary';
        //       textStr = 'Video Package';
        //     } else if (full.type === 'mobile') {
        //       classStr = 'label label-warning';
        //       textStr = 'Mobile Package';
        //     }
        //     resultStr = '<h4 class="text-center"><span class="' + classStr + '" title="' + VAM.j(textStr) + '">' + VAM.j(textStr) + '</span></h4>';
        //     return resultStr;
        //   }
        // },
        {
          'targets': [0,1,2],
          'render': function (data, type, full, meta) {
            var resultStr;
            resultStr = '<p title="' + data + '">' + data + '</p>';
            return resultStr;
          }
        },
        {
          'targets': -1,
          'orderable': false,
          'render': function (data, type, full, meta) {
            var resultStr = '<div class="tab-btn-group">'
                          + '<button class="btn btn-danger btn-sm tab-update" data-type="' + full.type + '" data-loading-text="<i class=\'fa fa-refresh fa-spin\'></i>" title="' + VAM.j('Update') + '"><i class="fa fa-refresh"></i> ' + VAM.j('Update') + '</button>'
                          + '</div>';
            return resultStr;
          }
        }
      ],
      'initComplete': function() {
        tableResize = new VamDataTable().columnResize($dataTable);
      }
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });
    // Table list render.
    listTable.on('draw.dt', function(evt) {
    });
    // Table list package update.
    $dataTable.on('click', 'tbody .tab-update', function(evt){
      var $me = $(this),
        opType = $me.data('type'),
        opUrl = dataSyncOp,
        opData;
      opData = {
        type: opType,
        rnd: (new Date()).getTime()
      };
      VamListOp.listOp({
        url: opUrl,
        data: opData,
        before: function() {
          $me.button('loading');
        },
        success: function(data) {
          VamOpMsg.messager(data);
        },
        always: function() {
          $me.button('reset');
          listTable.ajax.reload();
        }
      });
    });
  });
});